﻿// *****-------------------------------------------
// Author: Mariam Dholkawala
// Copyright 2010
// Countdown Timer for Games using Flash Lite 2.0
// *****-------------------------------------------

class Timers{
	private var tInterval:Number;//instance of timer
	private var duration:Number;// total ticker time in seconds
	private var countDown:Boolean;//should timer tick downwards?
	private var countStart:Number;//the actual countdown
	private var textName:TextField;//textfield name in the .fla file
	
	//Constructor reads the arguments and stores it in the class properties
	public function Timers(pDuration:Number, pCountDown:Boolean, pTextName:TextField){ 
		duration = pDuration; 
		countDown = pCountDown; 
		textName = pTextName;
		if(countDown){
			countStart = duration;
		}else{
			countStart = 0;
		}
	}
	//Starts the Timer
	public function startTimer(){
		tInterval = setInterval(this, 'playTimer', 1000);
		textName.text = String(countStart);
	}
	
	//This method is called everytime a second passes
	private function playTimer(){
		if(countDown){
			countStart--;
			if(countStart <= 0){
				stopTimer();
			}
		}else{
			countStart++;
			if(countStart >= duration){
				stopTimer();
			}
		}
		textName.text = String(countStart);
	}
	
	//Stops the timer
	private function stopTimer(){
		clearInterval(tInterval);
	}
}